package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Override
    public void editCheckGroup(CheckGroup group, List<Integer> list) {
        //编辑检查组
        checkGroupMapper.editCheckGroup(group);
        //删除检查组有关的检查项
        checkGroupMapper.deleteCheckGroupItem(group.getId());
        //新增检查组有关的检查项
        if(list != null && list.size() != 0) {
            checkGroupMapper.addCheckGroupItems(group.getId(), list);
        }
    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list = checkGroupMapper.findAll();
        return list;
    }

    @Override
    public void deleteById(Integer id) {
        //删除检查组
        checkGroupMapper.deleteCheckGroupById(id);
        //删除检查组有关的检查项
        checkGroupMapper.deleteCheckGroupItem(id);
    }

    /**
     * 分页查询检查项
     * @param pageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        Page<CheckItem> list = checkGroupMapper.findPage(pageBean.getQueryString());

        PageResult result = new PageResult(list.getTotal(),list.getResult());
        return result;
    }

    /**
     * 根据Id查询检查组
     * @param id
     * @return
     */
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    /**
     * 根据套餐id查询检查组id
     * @param id
     * @return
     */
    @Override
    public List<Integer> findCheckGroupIdsBySetmealId(Integer id) {
        List<Integer> list = checkGroupMapper.findCheckGroupIdsBySetmealId(id);
        return list;
    }

    @Autowired
    private CheckGroupMapper checkGroupMapper;

    /**
     * 增加检查组
     * @param group
     * @param list
     */
    @Override
    public void addCheckGroup(CheckGroup group, List<Integer> list) {
        //新增检查组
        checkGroupMapper.addCheckGroup(group);
        //处理包含的检查项
        Integer id = group.getId();
        if(list != null && list.size() != 0) {
            checkGroupMapper.addCheckGroupItems(id, list);
        }
    }
}
